package org.zfes.snowier.cloud.zbss.asf.engine;

import java.util.Optional;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicLong;

import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.DetectMode;

public class FaceEngineContainer {
	
	public Integer maxEngineSize=60;
	  
	private String appId;
	private String sdkKey;
	
	private LinkedBlockingQueue<FaceEngine> faceEngineQueue = new LinkedBlockingQueue<FaceEngine>();
	private AtomicLong engineQueueSize=new AtomicLong(0); 
	
	public  Optional<FaceEngine> take() {
		try {
			FaceEngine en=faceEngineQueue.take();
			engineQueueSize.decrementAndGet();
			
			return Optional.of(en);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return Optional.empty();
	}
	
	public  boolean add(FaceEngine faceEngine) {
		boolean isOk=faceEngineQueue.add(faceEngine);
		if(isOk) {
			engineQueueSize.incrementAndGet();
		}
		return isOk;
	}
	
    public void init() {
    	   EngineConfiguration engineConfiguration = EngineConfiguration.builder().detectMode(DetectMode.ASF_DETECT_MODE_IMAGE)
    			   .functionConfiguration(FunctionConfiguration.builder()
				                           .supportAge(true)
				                           .supportFace3dAngle(true)
				                           .supportFaceDetect(true)
				                           .supportFaceRecognition(true)
				                           .supportGender(true)
				                           .supportLiveness(true)
				                           .build())
    			   .build();
    	   
    	   
    	   for(int i=0;i< maxEngineSize;i++) {
    		   long start=System.currentTimeMillis();
    		   FaceEngine    faceEngine = new FaceEngine();
               //激活引擎
               faceEngine.active(appId, sdkKey);
               EngineConfiguration.builder().detectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
               //初始化引擎
               faceEngine.init(engineConfiguration);
               add( faceEngine);
               long end=System.currentTimeMillis();
               System.out.println("init -:"+i+ " 耗时 -:"+(end-start)/1000);
    	   }
          
    }
    
    private static FaceEngineContainer dataContainer;
	
	private FaceEngineContainer(String appId,String sdkKey,Integer maxEngineSize) {
		this.appId=appId;
		this.sdkKey=sdkKey;
		this.maxEngineSize=maxEngineSize;
		this.init();
	}

	public static FaceEngineContainer getInstance(String appId,String sdkKey,Integer maxEngineSize) {
	        if (dataContainer == null) {
	            synchronized (FaceEngineContainer.class) {
	            	if (dataContainer == null) {
	            		dataContainer = new FaceEngineContainer(appId,sdkKey,maxEngineSize);
	                }
	            }
	        }
	     return dataContainer;
	 }

	public Long getEngineQueueSize() {
		return engineQueueSize.longValue();
	}
	

	
}
